//
//  RSPredicateVC.m
//  RSTools
//
//  Created by thinkjoy on 2017/10/18.
//  Copyright © 2017年 杜瑞胜. All rights reserved.
//

#import "RSPredicateVC.h"
#import "BRPlaceholderTextView.h"

@interface RSPredicateVC ()<UIPickerViewDelegate,UIPickerViewDataSource,UITextViewDelegate>
{
    NSArray    * _dataAry;
    NSArray * _testStrAry;
}

@property (weak, nonatomic) IBOutlet BRPlaceholderTextView *regularExpressionTextV;
@property (weak, nonatomic) IBOutlet BRPlaceholderTextView *testTextV;

@property (weak, nonatomic) IBOutlet UILabel *resultLab;
@property (weak, nonatomic) IBOutlet UIPickerView *pickerV;


@end

@implementation RSPredicateVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _regularExpressionTextV.placeholder =   @"正则表达式";
    
    _testTextV.placeholder  =   @"测试字符串";
    
    _dataAry    =   @[
                      @{@"自定义输入":@"^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[a-zA-Z0-9\\W_]{8,}$"},
                      @{@"手机号":@"^1(3[0-9]|4[579]|5[0-35-9]|7[01356]|8[0-9])\\d{8}$"},
                      @{@"移动手机号":@"^1(34[0-8]|70[356]|(3[5-9]|4[7]|5[0-27-9]|7[8]|8[2-47-8])\\d)\\d{7}$"},
                      @{@"联通手机号":@"^1(70[07-9]|(3[0-2]|4[5]|5[5-6]|7[15-6]|8[5-6])\\d)\\d{7}$"},
                      @{@"电信手机号":@"^1(34[9]|70[0-2]|(3[3]|4[9]|5[3]|7[37]|8[019])\\d)\\d{7}$"},
                      @{@"IP":@"^((2([0-4]\\d|5[0-5])|[0-1]\\d{2})\\.){3}(2([0-4]\\d|5[0-5])|[0-1]?\\d{1,2})$"},
                      ];
    _testStrAry  =  @[
                      @"",
                      @"18288888888",
                      @"18240663517",
                      @"18240663517",
                      @"18240663517",
                      @"000.199.255.256"
                      ];
}
- (IBAction)check:(UIButton *)sender {
    _resultLab.text =   @"";
    
    if (_regularExpressionTextV.text.length>0 && _testTextV.text.length>0) {
        NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", _regularExpressionTextV.text];
        if ([regextestcu evaluateWithObject:_testTextV.text]) {
            _resultLab.text =   @"符合";
        }else{
            _resultLab.text =   @"不符合";
        }
    }else{
        _resultLab.text =   @"正则表达式或测试字符串为空";
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark    - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _dataAry.count;
}
#pragma mark    - UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSDictionary * dic  =   _dataAry[row];
    return dic.allKeys.firstObject;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _resultLab.text =   @"";
//    if (row==0) {
//        _regularExpressionTextV.text    =   @"";
//    }else{
//        _regularExpressionTextV.text    =   [[_dataAry[row] allValues] firstObject];
//    }
    _regularExpressionTextV.text    =   [[_dataAry[row] allValues] firstObject];
    _testTextV.text =   _testStrAry[row];
}

- (void)textViewDidChange:(UITextView *)textView
{
    _resultLab.text =   @"";
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
